// 互斥量的封装

#include <iostream>
#include <string.h>
#include <pthread.h>
#include<unistd.h>
namespace LockModule
{
    class Mutex
    {
    public:
        // 删除不要的拷贝和赋值
        Mutex(const Mutex &) = delete;
        const Mutex &operator = (const Mutex &) = delete;

        Mutex()
        {
            int n = pthread_mutex_init(&_mutex, nullptr);
            (void)n;
        }
        void Lock()
        {
            int n=pthread_mutex_lock(&_mutex);
            (void)n;
        }
        void UnLock()
        {
            int n=pthread_mutex_unlock(&_mutex);
            (void)n;
        }
        pthread_mutex_t* GetMutex()
        {
            return &_mutex;
        }
        ~Mutex()
        {
            int n=pthread_mutex_destroy(&_mutex);
            (void)n;
        }


    private:
    pthread_mutex_t _mutex;
    };

    //采用RAII风格对锁进行管理

    class LockGuard{
        public:
        //这里是对一个Mutext对象的引用，不存在创建一个新的Mutextd对象的操作
        //
        LockGuard(Mutex &mutex):_mutext(mutex)
        {
            _mutext.Lock();
        }
        ~LockGuard()
        {
            _mutext.UnLock();
        }

        public:
        Mutex &_mutext;
    };

} // namespace LockModule
